package com.weiqiao.api.rest;

import com.mingsoft.util.StringUtil;
import com.weiqiao.api.domain.dto.wx.ResponseUserInfo;
import com.weiqiao.api.until.RandomUtils;
import com.weiqiao.api.until.SHA1Utils;
import com.weiqiao.api.until.WxUtils;
import com.weiqiao.api.until.config.WXConfig;
import com.weiqiao.utils.RedisUtil;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ChenYF
 * @Description
 * @Date 2020/7/27 18:14
 */
@RestController
@RequestMapping("mallApi/wchart")
public class WChartApiController {
    @Autowired
    private WxUtils wxUtils;


    /**
     * 获取用户信息
     * 通过微信授权code ,获取微信用户信息
     * 二维码类型：0：商品分享 1：招募合伙人 2：团码
     *
     * @param code  微信返回的Code信息
     * @param param 其他参数  二维码类型,邀请人id,团队id
     * @return
     */
    @GetMapping("/authorizerlogin")
    public ResponseEntity authorizerLogin(
            @RequestParam("code") String code,
            @RequestParam("param") String param
    ) {
        Jedis jedis = RedisUtil.getJedis();
        String token = jedis.get("wchartProof:accesstoken");
        if (StringUtils.isBlank(token)) {
            wxUtils.getToken();
        }

        System.out.println(code + " == " + param);
        ResponseUserInfo user = null;
        if ((!StringUtil.isBlank(code))) {
            user = wxUtils.getWeixinUserInfo(code, param);
        }
        return new ResponseEntity(user, HttpStatus.OK);
    }

    /**
     * 绑定用户上下级
     *
     * @param user_id
     * @param param
     * @return
     */
    @GetMapping("/binding-recommend")
    public ResponseEntity bindingRecommend(
            @RequestParam("user_id") String user_id,
            @RequestParam("param") String param,
            @RequestParam("phone") String phone
    ) {
        ResponseUserInfo user = wxUtils.bindingUserInfo(user_id, param, phone);
        return new ResponseEntity(user, HttpStatus.OK);
    }

    /**
     * 刷新用户Token
     */
    @GetMapping("/get-refreshtoken")
    public ResponseEntity refreshToken(@RequestParam("refreshtoken") String refreshtoken) {
        return new ResponseEntity(wxUtils.getRefreshToken(refreshtoken), HttpStatus.OK);
    }

    /**
     * 返回ticket接口
     *
     * @return
     */
    @GetMapping("/get-ticket")
    public ResponseEntity getWxTicket() {
        Jedis jedis = RedisUtil.getJedis();
        String token = jedis.get("wchartProof:accesstoken");
        if (StringUtils.isBlank(token)) {
            wxUtils.getToken();
        }
        return new ResponseEntity(jedis.get("wchartProof:ticket"), HttpStatus.OK);
    }

    /**
     * 刷新AccessToken
     */
    @GetMapping("/refreshtoken")
    public void refreshToken() {
        wxUtils.getToken();
    }

    /**
     * 获取access Token
     *
     * @return
     */
    @GetMapping("/get-accesstoken")
    public ResponseEntity getAccessToken() {
        Jedis jedis = RedisUtil.getJedis();
        String token = jedis.get("wchartProof:accesstoken");
        if (StringUtils.isBlank(token)) {
            wxUtils.getToken();
        }
        Map<String, String> map = new HashMap<>();
        map.put("token", jedis.get("wchartProof:accesstoken"));
        return new ResponseEntity(map, HttpStatus.OK);
    }

    /**
     * 获取页面签名
     *
     * @return
     */
    @GetMapping("/get-signature")
    public ResponseEntity getSignature(@RequestParam("url") String url) throws Exception {
        Jedis jedis = RedisUtil.getJedis();
        String ticket = jedis.get("wchartProof:ticket");
        String noncestr = RandomUtils.generateString(16); // 16位随机字符串
        String timestamp = (System.currentTimeMillis() / 1000) + "";  // 时间撮
        if (StringUtil.isBlank(ticket)) {
            wxUtils.getToken();
        }
        String jsapi_ticket = ticket; // jsapi_ticket
        url = StringUtil.isBlank(url) ? WXConfig.JUMP_H5_URL : url;// 如果没有默认首页
        String ASCII = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + noncestr + "&timestamp=" + timestamp + "&url=" + url;
        Map<String, String> map = new HashedMap();
        map.put("noncestr", noncestr);
        map.put("timestamp", timestamp);
        map.put("signature", SHA1Utils.shaEncode(ASCII));
        return new ResponseEntity(map, HttpStatus.OK);
    }
}
